package com.orderplus.opshop.fetch.sina.weibo;

import com.orderplus.opshop.common.util.RegularUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestLoginWeibo {

	private static Pattern FM_VIEW = Pattern.compile("FM.view[(]");
	private static Pattern HTML = Pattern.compile("\"html\":\"<(\\S*?)[^>]*>.*?|<.*? />");

	public static void main(String[] args) throws IOException {
//		Pattern p = RegularUtil.SERIAL_PATTERN;

		Map<String, String> map = new HashMap<>();
		map.put("_s_tentry", "login.sina.com.cn");
		map.put("Apache", "8590250943507.381.1534758017640");
		map.put("login_sid_t", "0bd4e84c292620e3aed9862b4ad91b1c");
		map.put("SCF", "AnkI-DZbJ8fxr4V7JLMf5lSLqg70WTp3W4W08I4ImQYgJsZI4_K_wK3ga7HMv-Rn3zBvNuDUKeeqWreisfRJ4Hk.");
		map.put("SINAGLOBAL", "1793024353389.4553.1479174837212");
		map.put("SUB", "_2A252fvztDeRhGedK71YZ8SjPzj6IHXVVCmklrDV8PUNbmtBeLULGkW9NIehNIRmSpSekBJygSBwKLlN7g-Dw4Y_p");
		map.put("SUBP", "0033WrSXqPxfM725Ws9jqgMF55529P9D9WWFGdM31H.w2vFcAuWU4Nzw5JpX5K2hUgL.Fo2XShBReKq0SKz2dJLoIEnLxKML12zLB-eLxKML1hnLBo8gCH9ii--fi-2fi-z0i--Ni-2fiKL8");
		map.put("SUHB", "05iMWT8QM7LgFH");
		map.put("ULV", "1534758018626:10:3:1:8590250943507.381.1534758017640:1534586247687");
		map.put("un", "mascothaojun@sina.com");
		map.put("UOR", ",,login.sina.com.cn");
		map.put("TC-Page-G0", "046bedba5b296357210631460a5bf1d2");
//		map.put("TC-Ugrow-G0","e66b2e50a7e7f417f6cc12eec600f517");
//		map.put("TC-V5-G0","b8dff68fa0e04b3c8f0ba710d783479a");
		map.put("wb_bub_find", "1");
		map.put("wb_bub_find_3459285440", "1");
		map.put("wb_bub_find_5601838195", "1");
		map.put("WBStorage", "2c466cc84b6dda21|undefined");

		Connection.Response res = Jsoup.connect("https://d.weibo.com/102803?ssl_rnd=1534758103.9718").cookies(map).method(Connection.Method.GET).execute();

		String weiboHtml = getHtml(res.body());
//		System.out.println("weibo Html:" + weiboHtml);

		int start = weiboHtml.indexOf("<script charset=\"utf-8\">FM.view({\"ns\":\"pl_unlogin_home_feed\"");

		String subWeiboScript = weiboHtml.substring(start - 1, weiboHtml.length());
//		System.out.println("weibo subWeiboScript:" + subWeiboScript);


//		Document document = Jsoup.parse(weiboHtml);

//		Element myWeibo = document.getElementById("PCD_pictext_i_v5");

//		System.out.println("myWeibo： " + myWeibo.toString());

		System.out.println("{\n" +
				"    \"viewCount\": null,\n" +
				"    \"from\": {\n" +
				"        \"extend\": {\n" +
				"            \"bi_followers_count\": 2163,\n" +
				"            \"domain\": \"happyzhangjiang\",\n" +
				"            \"avatar_large\": \"https:\\/\\/tva3.sinaimg.cn\\/crop.2.0.176.176.180\\/a180de74jw1ev38avqrrtj2052052t96.jpg\",\n" +
				"            \"id\": 2709577332,\n" +
				"            \"city\": \"1000\",\n" +
				"            \"verified\": true,\n" +
				"            \"verified_reason\": \"知名搞笑幽默博主\",\n" +
				"            \"followers_count\": 9733096,\n" +
				"            \"location\": \"上海\",\n" +
				"            \"profile_url\": \"happyzhangjiang\",\n" +
				"            \"province\": \"31\",\n" +
				"            \"avatar_hd\": \"https:\\/\\/tva3.sinaimg.cn\\/crop.2.0.176.176.1024\\/a180de74jw1ev38avqrrtj2052052t96.jpg\",\n" +
				"            \"statuses_count\": 41337,\n" +
				"            \"description\": \"每日搜集整理发布各类新鲜资讯，欢迎私信\",\n" +
				"            \"friends_count\": 2286,\n" +
				"            \"idstr\": \"2709577332\",\n" +
				"            \"allow_all_act_msg\": true,\n" +
				"            \"allow_all_comment\": true,\n" +
				"            \"geo_enabled\": false,\n" +
				"            \"name\": \"Happy张江\",\n" +
				"            \"lang\": \"zh-cn\",\n" +
				"            \"favourites_count\": 5606,\n" +
				"            \"screen_name\": \"Happy张江\",\n" +
				"            \"url\": \"http:\\/\\/blog.sina.com.cn\\/happyzhangjiang\",\n" +
				"            \"gender\": \"f\",\n" +
				"            \"created_at\": \"Tue Feb 07 09:56:31 +0800 2012\"\n" +
				"        },\n" +
				"        \"name\": \"Happy张江\",\n" +
				"        \"friendCount\": 2286,\n" +
				"        \"url\": \"http:\\/\\/weibo.com\\/2709577332\\/F4hJSwLEc\",\n" +
				"        \"fansCount\": 9733096,\n" +
				"        \"likeCount\": 13,\n" +
				"        \"postCount\": 41337,\n" +
				"        \"id\": \"2709577332\",\n" +
				"        \"description\": \"每日搜集整理发布各类新鲜资讯，欢迎私信\"\n" +
				"    },\n" +
				"    \"mblog\": {\n" +
				"        \"reposts_count\": 84,\n" +
				"        \"retweeted_status\": {\n" +
				"            \"reposts_count\": 242,\n" +
				"            \"mblogid\": \"F4huN2DCH\",\n" +
				"            \"url\": \"http:\\/\\/weibo.com\\/5558366461\\/F4huN2DCH\",\n" +
				"            \"text\": \"中国留学生毕业演讲：美国的空气都是新鲜而甜美的！[并不简单]昨天美国马里兰大学举行毕业典礼，中国留学生代表Shuping Yang演讲，但是姑娘说：中国空气肮脏不堪，美国的却是如此香甜（Excuse me？！）[费解]http:\\/\\/t.cn\\/RadtoeY \u200B\u200B\u200B\",\n" +
				"            \"created_at\": \"Mon May 22 14:01:03 +0800 2017\",\n" +
				"            \"idstr\": \"4110205690628971\",\n" +
				"            \"mid\": \"4110205690628971\",\n" +
				"            \"attitudes_count\": 23,\n" +
				"            \"comments_count\": 244,\n" +
				"            \"user\": {\n" +
				"                \"bi_followers_count\": 3,\n" +
				"                \"domain\": \"\",\n" +
				"                \"avatar_large\": \"https:\\/\\/tva4.sinaimg.cn\\/crop.1.4.224.224.180\\/0064al1Xgw1evhjk8e89zj306c06st96.jpg\",\n" +
				"                \"id\": 5558366461,\n" +
				"                \"city\": \"1000\",\n" +
				"                \"verified\": false,\n" +
				"                \"verified_reason\": \"\",\n" +
				"                \"followers_count\": 1366127,\n" +
				"                \"location\": \"北京\",\n" +
				"                \"profile_url\": \"u\\/5558366461\",\n" +
				"                \"province\": \"11\",\n" +
				"                \"avatar_hd\": \"https:\\/\\/tva4.sinaimg.cn\\/crop.1.4.224.224.1024\\/0064al1Xgw1evhjk8e89zj306c06st96.jpg\",\n" +
				"                \"statuses_count\": 2326,\n" +
				"                \"description\": \"你开心就好\",\n" +
				"                \"friends_count\": 16,\n" +
				"                \"idstr\": \"5558366461\",\n" +
				"                \"allow_all_act_msg\": false,\n" +
				"                \"allow_all_comment\": true,\n" +
				"                \"geo_enabled\": true,\n" +
				"                \"name\": \"极品爆料王\",\n" +
				"                \"lang\": \"zh-cn\",\n" +
				"                \"favourites_count\": 0,\n" +
				"                \"screen_name\": \"极品爆料王\",\n" +
				"                \"url\": \"http:\\/\\/blog.sina.com\",\n" +
				"                \"gender\": \"m\",\n" +
				"                \"created_at\": \"Tue Mar 17 00:44:06 +0800 2015\"\n" +
				"            },\n" +
				"            \"id\": 4110205690628971,\n" +
				"            \"geo\": null,\n" +
				"            \"isLongText\": false\n" +
				"        },\n" +
				"        \"mblogid\": \"F4hJSwLEc\",\n" +
				"        \"text\": \"[拜拜]\\/\\/@要和太阳肩并肩的小霸王萝莉: 跪舔的嘴脸真丑……\\/\\/@努力奔跑的stone: 小姑娘祝你好运 \\/\\/@得瑟米: 我靠[鄙视][吃惊][蜡烛]\",\n" +
				"        \"created_at\": \"Mon May 22 14:38:14 +0800 2017\",\n" +
				"        \"idstr\": \"4110215047809656\",\n" +
				"        \"mid\": \"4110215047809656\",\n" +
				"        \"attitudes_count\": 13,\n" +
				"        \"comments_count\": 35,\n" +
				"        \"id\": 4110215047809656,\n" +
				"        \"geo\": null,\n" +
				"        \"isLongText\": false\n" +
				"    },\n" +
				"    \"title\": null,\n" +
				"    \"url\": \"http:\\/\\/weibo.com\\/2709577332\\/F4hJSwLEc\",\n" +
				"    \"pDate\": \"2017-05-22 14:38:14\",\n" +
				"    \"likeCount\": 13,\n" +
				"    \"commentCount\": 35,\n" +
				"    \"shareCount\": 84,\n" +
				"    \"content\": \"[拜拜]\\/\\/@要和太阳肩并肩的小霸王萝莉: 跪舔的嘴脸真丑……\\/\\/@努力奔跑的stone: 小姑娘祝你好运 \\/\\/@得瑟米: 我靠[鄙视][吃惊][蜡烛]\",\n" +
				"    \"imageURLs\": null,\n" +
				"    \"id\": \"4110215047809656\",\n" +
				"    \"videoURLs\": null\n" +
				"}");

		Matcher scriptMatcher = FM_VIEW.matcher(subWeiboScript);

		if (scriptMatcher.find()) {
			String matchGroup = getVal(subWeiboScript,HTML);

			System.out.println("matchGroup: " + matchGroup);
		}
	}

	public static String getVal(String str,Pattern p){
		Matcher m = p.matcher(str);
		if (m.find()){
			return m.group().trim();
		}
		return null;
	}

	public static String getHtml(String s) {
		String content = s
				.replaceAll("(\\\\t|\\\\n|\\\\r)", "")
//				.replaceAll("\\\\\"", "\"")
//				.replaceAll("\\\\/", "/")
//				.replaceAll("<!--", "")
//				.replaceAll("-->", "")
				;

		content = content.substring(0, content.length() <= 13 ? content.length() : content.length() - 13);
		return Native2AsciiUtils.ascii2Native(content);
	}
}
